﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Capstone_project.DatabaseAccessLayer;
using Capstone_project.BussnessLogicLayer;

namespace Capstone_project
{
    public partial class frmAddTask : Form
    {
        public frmAddTask()
        {
            InitializeComponent();
        }
        string project123;
        SqlDataAdapter adap;
        SqlConnection con = Common.con;
        DataTable dttaskgroup = new DataTable();
        DataTable dtproject = new DataTable();
        TaskBUS taskbus = new TaskBUS();
        ProjectBUS projectbus=new ProjectBUS();
        TaskGroupBUS taskgroupbus = new TaskGroupBUS();
        PriorityBUS prioritybus = new PriorityBUS();
        TaskStatusBUS taskstatusbus = new TaskStatusBUS();
        public static string idtaskgroup;
        public static string idproject;
        private void frmAddTask_Load(object sender, EventArgs e)
        {
            
            txtNameTask.Focus();
            txtIDTask.Text = taskbus.nextID();
            txtIDTask.Enabled = false;
            cbxPriority.DataSource = prioritybus.getList();
            cbxPriority.DisplayMember = "NAME_PRIORITY";
            cbxPriority.ValueMember = "ID_PRIORITY";

            if(idproject != null && idtaskgroup==null)
            {
                string sqlproject = "SELECT * FROM PROJECT WHERE ID_PROJECT='"+idproject+"'";
                if (con.State != ConnectionState.Open)
                    con.Open();
                SqlCommand cmd1 = new SqlCommand(sqlproject, con);
                adap = new SqlDataAdapter(cmd1);
                adap.Fill(dtproject);
                con.Close();
                cbxProject.DataSource = dtproject;
                cbxProject.DisplayMember = "NAME_PROJECT";
                cbxProject.ValueMember = "ID_PROJECT";

                string sqlgrouptask = "SELECT     dbo.TASK_GROUP.ID_TASK_GROUP, dbo.TASK_GROUP.NAME_TASK_GROUP, dbo.TASK_GROUP.ID_PROJECT"
                +" FROM         dbo.TASK_GROUP INNER JOIN"
                +"       dbo.PROJECT ON dbo.TASK_GROUP.ID_PROJECT = dbo.PROJECT.ID_PROJECT"
                +" WHERE     (dbo.TASK_GROUP.ID_PROJECT = '"+idproject+"')";
                if (con.State != ConnectionState.Open)
                    con.Open();
                SqlCommand cmd2 = new SqlCommand(sqlgrouptask, con);
                adap = new SqlDataAdapter(cmd2);
                adap.Fill(dttaskgroup);
                con.Close();
                cbxTaskGroup.DataSource = dttaskgroup;
                cbxTaskGroup.DisplayMember = "NAME_TASK_GROUP";
                cbxTaskGroup.ValueMember = "ID_TASK_GROUP";
            }
            else if (idproject != null && idtaskgroup != null)
            {
                string sqlproject = "SELECT * FROM PROJECT WHERE ID_PROJECT='" + idproject + "'";
                if (con.State != ConnectionState.Open)
                    con.Open();
                SqlCommand cmd1 = new SqlCommand(sqlproject, con);
                adap = new SqlDataAdapter(cmd1);
                adap.Fill(dtproject);
                con.Close();
                cbxProject.DataSource = dtproject;
                cbxProject.DisplayMember = "NAME_PROJECT";
                cbxProject.ValueMember = "ID_PROJECT";

                string sqlgrouptask = "SELECT * FROM TASK_GROUP WHERE ID_TASK_GROUP='"+idtaskgroup+"'";
                if (con.State != ConnectionState.Open)
                    con.Open();
                SqlCommand cmd2 = new SqlCommand(sqlgrouptask, con);
                adap = new SqlDataAdapter(cmd2);
                adap.Fill(dttaskgroup);
                con.Close();
                cbxTaskGroup.DataSource = dttaskgroup;
                cbxTaskGroup.DisplayMember = "NAME_TASK_GROUP";
                cbxTaskGroup.ValueMember = "ID_TASK_GROUP";
            }

            else if (idproject == null && idtaskgroup != null)
            {
                string sqlproject = "SELECT     dbo.PROJECT.*"
                + " FROM         dbo.TASK_GROUP INNER JOIN"
                + "      dbo.PROJECT ON dbo.TASK_GROUP.ID_PROJECT = dbo.PROJECT.ID_PROJECT"
                + "    where TASK_GROUP.ID_TASK_GROUP='"+idtaskgroup+"'";
                if (con.State != ConnectionState.Open)
                    con.Open();
                SqlCommand cmd1 = new SqlCommand(sqlproject, con);
                adap = new SqlDataAdapter(cmd1);
                adap.Fill(dtproject);
                con.Close();
                cbxProject.DataSource = dtproject;
                cbxProject.DisplayMember = "NAME_PROJECT";
                cbxProject.ValueMember = "ID_PROJECT";

                string sqlgrouptask = "SELECT     dbo.TASK_GROUP.ID_TASK_GROUP, dbo.TASK_GROUP.NAME_TASK_GROUP, dbo.TASK_GROUP.ID_PROJECT"
                + " FROM         dbo.TASK_GROUP INNER JOIN"
                + "       dbo.PROJECT ON dbo.TASK_GROUP.ID_PROJECT = dbo.PROJECT.ID_PROJECT"
                + " WHERE     (dbo.TASK_GROUP.ID_PROJECT = '" + cbxProject.SelectedValue + "')";
                if (con.State != ConnectionState.Open)
                    con.Open();
                SqlCommand cmd2 = new SqlCommand(sqlgrouptask, con);
                adap = new SqlDataAdapter(cmd2);
                adap.Fill(dttaskgroup);
                con.Close();
                cbxTaskGroup.DataSource = dttaskgroup;
                cbxTaskGroup.DisplayMember = "NAME_TASK_GROUP";
                cbxTaskGroup.ValueMember = "ID_TASK_GROUP";
            }
            
            else if (idproject == null && idtaskgroup == null)
            {
                string sqlproject = "SELECT * FROM PROJECT";
                if (con.State != ConnectionState.Open)
                    con.Open();
                SqlCommand cmd1 = new SqlCommand(sqlproject, con);
                adap = new SqlDataAdapter(cmd1);
                adap.Fill(dtproject);
                con.Close();
                cbxProject.DataSource = dtproject;
                cbxProject.DisplayMember = "NAME_PROJECT";
                cbxProject.ValueMember = "ID_PROJECT";

                string sqlgrouptask = "SELECT     dbo.TASK_GROUP.ID_TASK_GROUP, dbo.TASK_GROUP.NAME_TASK_GROUP, dbo.TASK_GROUP.ID_PROJECT"
                + " FROM         dbo.TASK_GROUP INNER JOIN"
                + "       dbo.PROJECT ON dbo.TASK_GROUP.ID_PROJECT = dbo.PROJECT.ID_PROJECT"
                + " WHERE     (dbo.TASK_GROUP.ID_PROJECT = '" + idproject + "')";
                if (con.State != ConnectionState.Open)
                    con.Open();
                SqlCommand cmd2 = new SqlCommand(sqlgrouptask, con);
                adap = new SqlDataAdapter(cmd2);
                adap.Fill(dttaskgroup);
                con.Close();
                cbxTaskGroup.DataSource = dttaskgroup;
                cbxTaskGroup.DisplayMember = "NAME_TASK_GROUP";
                cbxTaskGroup.ValueMember = "ID_TASK_GROUP";
            }

            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (txtIDTask.Text == "" || txtNameTask.Text == "" || cbxTaskGroup.Text=="" || txtTimes.Text==""||cbxPriority.Text=="")
            {
                MessageBox.Show("You must enter all infomation","",MessageBoxButtons.OK,MessageBoxIcon.Warning);
            }

            else
            {
                string idtask = txtIDTask.Text;
                string nametask = txtNameTask.Text;
                string idtaskstatus = "ATS003";
                idtaskgroup = cbxTaskGroup.SelectedValue.ToString();
                
                string idpriority = cbxPriority.SelectedValue.ToString();
                float times = float.Parse(txtTimes.Text);
                if (con.State != ConnectionState.Open)
                    con.Open();
                SqlCommand cmd = new SqlCommand("AddnewTask",con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@idtask", SqlDbType.NChar).Value = idtask;
                cmd.Parameters.Add("@nametask", SqlDbType.NVarChar).Value = nametask;
                cmd.Parameters.Add("@idtaskstatus", SqlDbType.NChar).Value = idtaskstatus;
                cmd.Parameters.Add("@idtaskgroup", SqlDbType.NChar).Value = idtaskgroup;
                cmd.Parameters.Add("@idpriority", SqlDbType.NChar).Value = idpriority;
                cmd.Parameters.Add("@times", SqlDbType.NChar).Value = times;
                cmd.Parameters.Add("@complete", SqlDbType.NChar).Value = 0;
                try
                {
                    int kq = cmd.ExecuteNonQuery();
                    if (kq != 0)
                    {
                        MessageBox.Show("Task added successfully !");
                        idtaskgroup = null;
                        this.Close();
                        frmAddNewTask addnewtask = new frmAddNewTask();
                        addnewtask.frmAddNewTask_Load(null,null);

                    }
                }
                catch { MessageBox.Show("Please check again task information."); }
                con.Close();
            }
        }
        #region Chặn ko cho nhập chữ. Chỉ cho nhập số và dấu '.'

        private void txtTimes_KeyPress_1(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != '.' ||
                (e.KeyChar == '.' && (txtTimes.Text.Length == 0 || txtTimes.Text.IndexOf('.') != -1))))
                e.Handled = true;
        }

        #endregion

        //Module thay đổi theo Project tương ứng
        private void cbxProject_SelectedIndexChanged(object sender, EventArgs e)
        {
            project123 = cbxProject.SelectedValue.ToString();
            cbxTaskGroup.DropDownStyle = ComboBoxStyle.DropDownList;
            cbxTaskGroup.Enabled = true;
            cbxTaskGroup.DataSource = taskgroupbus.getTaskGrBYPROJECT(project123);
            cbxTaskGroup.DisplayMember = "NAME_TASK_GROUP";
            cbxTaskGroup.ValueMember = "ID_TASK_GROUP";
            
        }
    }
}
